<?php
/*
*微信类
*/
class wxClass{
	/*微信登录*/
	public function wxLogin($thisUrl){
		//赋值
		$wxGetOpenid = wxGetOpenid($thisUrl);
		$wxGetData = wxGetData($wxGetOpenid['access_token'],$wxGetOpenid['openid']);
		$result = array();
		$time = date("Y-m-d H:i:s");
		//判断
		if(!empty($wxGetOpenid['warn'])){
			$result['warn'] = $wxGetOpenid['warn'];
		}elseif(!empty($wxGetData['warn'])){
			$result['warn'] = $wxGetData['warn'];
		}else{
			$wxData = wxData($wxGetData['openid']);
			//通过openid找对应的用户是否注册
			$kehu = query("kehu"," wxOpenid = '$wxGetData[openid]' ");
			//如果查询不到说明没有注册
			if(empty($kehu['khid'])){
				$khid = suiji();
				$field = array(
					"khid" => $khid,
					"Source" => "微信自动注册",
					"wxOpenid" => $wxGetData['openid'],
					"wxSex" => $wxGetData['sex'],
					"wxNickName" => $wxGetData['nickname'],
					"wxAddress" => $wxGetData['address'],
					"wxIco" => $wxGetData['ico'],
					"wxFollow" => $wxData['subscribe'],
					"updateTime" => $time,
					"time" => $time,
				);
				$warn = insert(array("table" => "kehu","field" => $field));
				if($warn == "新增成功"){
					$_SESSION['khid'] = $khid;
				}else{
					$result['warn'] = "insert kehu is error";
				}
			//查询到了，说明客户注册则为客户登录并随着客户资料的变动而更新
			}else{
				$field = array("wxSex" => $wxGetData['sex'],"wxNickName" => $wxGetData['nickname'],"wxAddress" => $wxGetData['address'],"wxIco" => $wxGetData['ico'],"wxFollow" => $wxData['subscribe'],"updateTime" => $time);
				$warn = update(array("table" => "kehu","field" => $field,"where" => " khid = '$kehu[khid]' "));
				if($warn == "更新成功"){
					$_SESSION['khid'] = $kehu['khid'];
				}else{
					$result['warn'] = " update kehu is error ";	
				}
			}
		}
		//返回
		return $result;
	}
	/*
	*网页形式获取用户微信基本信息
	*openid：$user['openid']
	*用户昵称:$user['nickname']
	*用户性别$user['sex']
	*用户头像$user['ico']
	*用户地址$user['address']
	*/
	public function wxGetData($access_token,$openid){
		$result = array();
		if(empty($access_token)){
			$result['warn'] = " not access_token ";
		}elseif(empty($openid)){
			$result['warn'] = " not openid ";
		}else{
			$umes_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
			$user = json_decode(self::curlGet($umes_url),true);
			//用户openid
			$result['openid'] = $openid;
			//用户昵称
			$result['nickname'] = $user['nickname'];
			//用户性别
			if($user['sex'] == 1){
				$result['sex'] = "男";
			}else if($user['sex'] == 2){
				$result['sex'] = "女";
			}else if($user['sex'] == 0){
				$result['sex'] = "未知";
			}
			//用户头像
			$result['ico'] = $user['headimgurl'];
			//用户地址
			$result['address'] = $user['country'].$user['province'].$user['city'];
		}
		//返回
		return $result;
	}
	/*********微信自动登录注册函数**************************
	开发文档：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455784140&token=&lang=zh_CN
	1、修改授权回调域名：开发 - 接口权限 - 网页服务 - 网页授权 - 网页授权获取用户基本信息，如www.yumukeji.com
	$thisUrl为当前页面路径
	*/
	public function wxCode($thisUrl){
		$result = array();
		if(empty($_GET['code'])){
			$appId = para("wxAppid");
			if(empty($appId)){
				$result['warn'] = " not appId ";
			}else{
				$thisUrl = urlencode($thisUrl);
				$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$thisUrl."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
				header("location:".$url);
				exit(0);
			}
		}else{
			$result['code'] = $_GET['code'];
		}
		return $result;
	}
	//返回值$token['openid']和$token['access_token']
	public function wxGetOpenid($thisUrl){
		//赋值
		$appId = para("wxAppid");
		$appSecret = para("wxAppSecret");
		$wxCode = $this->wxCode($thisUrl);
		$result = array();
		//判断
		if(empty($appId)){
			$result['warn'] = " not appId ";
		}elseif(empty($appSecret)){
			$result['warn'] = " not appSecret ";
		}elseif(empty($wxCode['code'])){
			if(empty($wxCode['warn'])){
				$result['warn'] = " not code ";
			}else{
				$result['warn'] = $wxCode['warn'];
			}
		}else{
			$tokenJson = self::curlGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$wxCode['code']."&grant_type=authorization_code");
			$token = json_decode($tokenJson,true);
			if(empty($token['openid']) or empty($token['access_token'])){
				$result['warn'] = $tokenJson;
			}else{
				$result['openid'] = $token['openid'];
				$result['access_token'] = $token['access_token'];
			}
		}
		return $result;
	}
	/*********通过微信openid获取客户信息**************************/
	//必须要关注公众号才能使用此函数获得客户信息
	public function wxData($openid){
		$data['openid'] = $openid;
		$access_token = access_token();
		$UserJson = self::curlGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN");
		$user = json_decode($UserJson,true);
		if($user['subscribe'] == 1){
			$data['subscribe'] = "是";//是否关注微信公众号
			if($user['sex'] == 1){
				$data['sex'] = "男";
			}else if($user['sex'] == 2){
				$data['sex'] = "女";
			}
			$data['nickname'] = $user['nickname'];
			$data['ico'] = $user['headimgurl'];//头像地址
			$data['address'] = $user['country'].$user['province'].$user['city'];
		}else{
			$data['subscribe'] = "否";
		}
		return $data;
	}
	/***********************获取access_token************************/
	public function access_token(){
		$wxAppid = para("wxAppid");//微信appid
		$wxAppSecret = para("wxAppSecret");//微信wxAppSecret
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$wxAppid."&secret=".$wxAppSecret;
		$token = json_decode(curl($url,""),true);
		$access_token = $token['access_token'];
		return $access_token;
	}
}
?>